import { _decorator, ccenum, CCFloat, CCInteger, Component, Enum, instantiate, Node, Prefab, setPropertyEnumType, Vec2 } from 'cc';
import { Skill } from '../Skill';
import { Sword_Skill_Controller } from './Sword_Skill_Controller';
import { Player } from '../../Actor/Player/Player';
const { ccclass, property } = _decorator;


export enum SwordType {
    Regular,
    Bounce,
    Pierce,
    Spin
} ccenum(SwordType)

@ccclass('Sword_Skill')
export class Sword_Skill extends Skill {
    @property({ type: SwordType }) swordType: SwordType = SwordType.Regular;

    @property(CCInteger) private pierceAmount: number;
    @property(CCInteger) private bounceAmount: number;
    @property(CCFloat) private bounceGravity: number;

    @property(CCFloat) private piercedGravity: number;

    @property(CCFloat) private maxTravelDistance: number = 7;
    @property(CCFloat) private spinDuration: number = 3;
    private spinTimer: number;
    private wasStopped: boolean;
    @property(CCFloat) private spinGravity: number = 2;
    private hitCoolDown: number = 0.3;

    @property(CCFloat) private swordGravity: number;
    @property(Prefab) swordPrefab: Prefab = null;
    @property(Vec2) launchDir: Vec2 = new Vec2(0, 0);

    private finalDir: Vec2;

    protected update(dt: number): void {

    }
    protected start(): void {
        super.start();

        this.setUpGravity();
    }
    private setUpGravity() {
        if (this.swordType == SwordType.Bounce) {
            this.swordGravity = this.bounceGravity;
        } else if (this.swordType == SwordType.Pierce) {
            this.swordGravity = this.piercedGravity;
        } else {
            this.swordGravity = this.spinGravity;
        }
    }

    CreateSword() {
        const sword = instantiate(this.swordPrefab);
        this.player.AssignNewSword(sword);
        this.node.addChild(sword);
        sword.setWorldPosition(this.player.node.getWorldPosition());
        const SwordController = sword.getComponent(Sword_Skill_Controller);
        if (this.swordType == SwordType.Bounce) {
            SwordController.setupBounce(true, this.bounceAmount);
        } else if (this.swordType == SwordType.Pierce) {
            SwordController.setupPierce(this.pierceAmount);
        } else if (this.swordType == SwordType.Spin) {
            SwordController.setupSpin(true,this.maxTravelDistance,this.spinDuration,this.hitCoolDown);
        }

        SwordController.setUpSword(this.launchDir.clone().multiply2f(this.player.facingDir, 1), this.swordGravity, this.player);
    }
}




